//
//  DailyStoryViewController.m
//  GOGOTravel
//
//  Created by dllo on 16/1/25.
//  Copyright © 2016年 果果. All rights reserved.
//

#import "DailyStoryViewController.h"
#import "DailyFeatureStoryViewController.h"
@interface DailyStoryViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) NSMutableArray *dailyStoryArr;/**< 故事精选 */

@property (nonatomic, retain) BaseCollectionView *collection;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) BOOL isHeader;
@end

@implementation DailyStoryViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}


- (void)dealloc{
    [_model release];
    [_dailyStoryArr release];
    [_collection release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.9 green:0.29 blue:0.44 alpha:1];
    self.view.backgroundColor = [UIColor redColor];
    self.navigationItem.title = @"精选故事";
    
    [self handleDailyStory];
    [self initTableView];
    
    self.dailyStoryArr = [NSMutableArray array];
    _page = 0;
    
    [self addFooterRef];
    [self createHeaderRef];
    
}

- (void)createHeaderRef{
    
    [_collection addHeaderWithCallback:^{
        _page = 0;
        [self handleDailyStory];
        _isHeader = YES;
    }];
    
}

- (void)addFooterRef{
   
    [_collection addFooterWithCallback:^{
       
        
        _page = _page + 12;
        
        [self handleDailyStory];
        _isHeader = NO;

    }];
    
    
}


- (void)initTableView{
   
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    layout.itemSize = CGSizeMake((WIDTH - 30) / 2, 250);
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.collection = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT ) collectionViewLayout:layout];
    _collection.delegate = self;
    _collection.dataSource = self;
    
    //关闭collectionView的交互
    // _collection.userInteractionEnabled = NO;
   // _collection.backgroundColor = UIColorFromRGB(0xF0F0F0);
    
    [self.view addSubview:_collection];
    
    [_collection release];
    //注册
    [_collection registerClass:[DailyCollectionViewCell class] forCellWithReuseIdentifier:@"DailyCollectionViewCell1"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dailyStoryArr.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DailyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DailyCollectionViewCell1"  forIndexPath:indexPath];
    Daily *model = _dailyStoryArr[indexPath.row];
   
    cell.layer.cornerRadius = 10;
    cell.layer.masksToBounds = YES;
    cell.model = model;
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    //取出项目的sopt_id 传到下一个页面
    //取出项目的sopt_id 传到下一个页面
    Daily *model = _dailyStoryArr[indexPath.row];
    NSDictionary *dataDic = model.data[0];
    NSString *spot_id = dataDic[@"spot_id"];
    
    ShouChangModel *shouChangModel = [[ShouChangModel alloc] init];
    [shouChangModel setValue:dataDic[@"index_title"] forKey:@"titleLabelStr"];
    [shouChangModel setValue:dataDic[@"index_cover"] forKey:@"backImageStr"];
    NSDictionary *user = dataDic[@"user"];
    
    [shouChangModel setValue:user[@"name"] forKey:@"nameLabelStr"];
    [shouChangModel setValue:user[@"avatar_l"] forKey:@"hearderImageStr"];
    
    
    NSNumber *num = dataDic[@"spot_id"];
    NSString *tripId = [NSString stringWithFormat:@"%@",num];
    
    [shouChangModel setValue:tripId forKey:@"spot_id"];
    
    
    DailyFeatureStoryViewController *dfs = [[DailyFeatureStoryViewController alloc] init];
    dfs.url = [NSString stringWithFormat:@"%@", spot_id];
    dfs.model = model;
    dfs.shouChangModel = shouChangModel;
    
    [self.navigationController pushViewController:dfs animated:YES];
    [dfs release];
    
    
    
}



/** 数据处理精选故事更多 */

- (void)handleDailyStory{
    
    NSString *str = [NSString stringWithFormat:@"http://api.breadtrip.com/v2/new_trip/spot/hot/list/?start=%ld", (long)_page];
    
    [AFNetWorkingHandle GETRequestWithURLString:str completionWithSuccess:^(id result) {
        
        NSDictionary *dic = result;
        
        NSDictionary *dic1= dic[@"data"];
        
        NSArray *arr = dic1[@"hot_spot_list"];
        //        NSLog(@"164564f6546846465a4d65f4a%@", arr);
        
        if (_isHeader == YES) {
            [_dailyStoryArr removeAllObjects];
        }
        
        
        NSMutableArray *arrResult = [NSMutableArray array];
        
        for (NSDictionary *dic in arr) {
            NSMutableArray *arr = [NSMutableArray array];
            
            [arr addObject:dic];
            
            Daily *model = [[Daily alloc] init];
            [model setValue:arr forKey:@"data"];
            
            [arrResult addObject:model];
        }
        
        
        for (Daily *model in arrResult) {
            [_dailyStoryArr addObject:model];
        }
        
        
        
        [_collection reloadData];
        
        
        [_collection footerEndRefreshing];
        [_collection headerEndRefreshing];
        
    } withFailure:^(id error) {
        NSLog(@"%@", error);
    }];
    
    
    
    
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
